package com.situ.mallbrand.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.model.Product;
import com.situ.mallbrand.dao.BrandMapper;
import com.situ.mallbrand.model.Brand;
import com.situ.mallbrand.service.BrandService;
import com.situ.mallbrand.service.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {



    @Autowired
    public BrandMapper brandMapper;

    @Autowired
    public ProductFeignClient productFeignClient;

    @Override
    public Boolean create(Brand  brand) {

        //基础校验与默认值
        if(!StringUtils.hasText(brand.getName())) {
            throw new IllegalArgumentException("品牌名称不能为空");
        }

        //如果未提供slug 则根据name生成
        if(!StringUtils.hasText(brand.getSlug())) {
            brand.setSlug(brand.getName());
        }
        if(brand.getStatus() == null) {
            brand.setStatus(1);
        }
        if(brand.getSortOrder() == null) {
            throw new IllegalArgumentException("排序字段不能为空");
        }

        return brandMapper.insert(brand) > 0;

    }

    @Override
    public boolean updateById(Brand brand) {
        if(brand.getId() == null) {
            throw new IllegalArgumentException("id不能为空");
        }
        //仅更新传入的字段
        return brandMapper.updateById(brand) > 0;
    }

    @Override
    public Brand getById(Long id) {
        //查询时忽略软删除
        Brand found = brandMapper.selectById(id);
        if(found == null) {
            return null;
        }
        return found.getDeletedAt() == null ? found : null;
    }

    @Override
    public Page<Brand> pageQuery(String keyword, Integer status, Integer page, Integer size) {
       //守卫页码与大小
        int safePage = page <= 0 ? 1 : page;
        int safeSize = size <= 0 ? 10 : size;

        //条件构造
        // - 状态精准匹配
        // - 关键字匹配name或者slug
        // - 过滤软删除
        // - 排序：sort_order ASC
        LambdaQueryWrapper<Brand> qw = new LambdaQueryWrapper<>();
        qw.eq(status != null, Brand::getStatus, status);
        if(StringUtils.hasText(keyword)) {
            qw.and(w -> w.like(Brand::getName, keyword)
                    .or()
                    .like(Brand::getSlug, keyword)
            );

        }
        qw.isNull(Brand::getDeletedAt);
        qw.orderByAsc(Brand::getSortOrder).orderByDesc(Brand::getId);

        return brandMapper.selectPage(Page.of(safePage, safeSize),qw);
    }

    @Override
    public boolean softDelete(Long id) {
        //软删除 只更新deleted_at字段
       Brand b = new Brand();
       b.setId(id);
       b.setDeletedAt(LocalDateTime.now());
       return brandMapper.updateById(b) > 0;
    }

    @Override
    public List<Brand> getBrandList() {
        LambdaQueryWrapper<Brand> qw = new LambdaQueryWrapper<>();
        qw.eq(Brand::getStatus,1).isNull(Brand::getDeletedAt);

       return brandMapper.selectList(qw);
    }

    @Override
    public List<Product> getProductsByBrand(Long brandId) {
        return productFeignClient.getProductsByBrand(brandId);
    }


    /**
     * 简易 slug 生成：小写、空白替换为短横线
     */
    private String generateSlug(String name) {
        return name == null ? null : name.trim().toLowerCase().replaceAll("\\s+", "-");
    }
}
